package com.ferry.lock.lock.impl;

import com.ferry.lock.engine.LockEngine;
import com.ferry.lock.lock.IReentrantLock;
import com.ferry.lock.lock.LockBase;

import java.util.concurrent.TimeUnit;

/**
 * 重入锁
 * Created by daiyong
 */
public class ReentrantLock extends LockBase implements IReentrantLock {

	private IReentrantLock iReentrantLock;

	public ReentrantLock(LockEngine lockEngine, String lockName) {
		super(lockEngine, lockName, DEFAULT_EXPIRE_TIME, DEFAULT_RETRY);
		this.iReentrantLock = lockEngine.getReentrantLock(lockName);
	}

	public ReentrantLock(LockEngine lockEngine, String lockName, int expireTime) {
		super(lockEngine, lockName, expireTime, DEFAULT_RETRY);
		this.iReentrantLock = lockEngine.getReentrantLock(lockName, expireTime);
	}

	public ReentrantLock(LockEngine lockEngine, String lockName, int expireTime, int retry) {
		super(lockEngine, lockName, expireTime, retry);
		this.iReentrantLock = lockEngine.getReentrantLock(lockName, expireTime, retry);
	}

	@Override
	public void lock() {
		iReentrantLock.lock();
	}

	@Override
	public boolean tryLock() {
		return iReentrantLock.tryLock();
	}

	@Override
	public boolean tryLock(long time, TimeUnit timeUnit) throws InterruptedException {
		return iReentrantLock.tryLock(time, timeUnit);
	}

	@Override
	public void lockInterruptibly() throws InterruptedException {
		iReentrantLock.lockInterruptibly();
	}

	@Override
	public void unLock() {
		iReentrantLock.unLock();
	}

	@Override
	public String getName() {
		return lockName;
	}

	@Override
	public void switchLock() {
		this.iReentrantLock = lockEngine.getReentrantLock(this.lockName, this.expireTime, this.retry);
	}

}
